package com.opensource.component.query.script;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import java.util.stream.Stream;

/**
 * @author ZL since on 2021/12/23 下午3:54
 */
public enum ExtendMybatisCommand implements MybatisCommand {


  /**
   * 查询
   */
  SELECT_LIST_EXTEND("selectListExtend", "查询满足条件所有数据",
      "<script>%s SELECT %s %s FROM %s %s %s %s\n</script>"),
  SELECT_COUNT_EXTEND("selectCountExtend", "查询满足条件的总数",
      "<script>%s SELECT COUNT(1) FROM %s %s %s %s\n</script>"),
  SELECT_PAGE_EXTEND("selectPageExtend", "查询满足条件的列表分页",
      "<script>%s SELECT %s %s FROM %s %s %s %s\n</script>"),

  CLICKHOUSE_SELECT_LIST_EXTEND("selectClickhouseListExtend", SqlMethod.SELECT_LIST.getDesc(),
      SqlMethod.SELECT_LIST.getSql()),
  CLICKHOUSE_COUNT_EXTEND("selectClickhouseCountExtend", SqlMethod.SELECT_COUNT.getDesc(),
      SqlMethod.SELECT_COUNT.getSql()),
  CLICKHOUSE_PAGE_EXTEND("selectClickhousePageExtend", SqlMethod.SELECT_PAGE.getDesc(),
      SqlMethod.SELECT_PAGE.getSql()),

  CLICKHOUSE_INSERT_EXTEND("insertClickhouseExtend", SqlMethod.INSERT_ONE.getDesc(),
      SqlMethod.INSERT_ONE.getSql()),
  //<script>
  // ALTER TABLE user_info UPDATE <>
  //<if test="et != null">
  //<if test="et['userName'] != null">user_name=#{et.userName},</if>
  //<if test="et['age'] != null">age=#{et.age},</if>
  //<if test="et['creationTime'] != null">creation_time=#{et.creationTime},</if>
  //<if test="et['creator'] != null">creator=#{et.creator},</if>
  //<if test="et['pubts'] != null">pubts=#{et.pubts},</if>
  //<if test="et['modifier'] != null">modifier=#{et.modifier},</if>
  //<if test="et['modifiedTime'] != null">modified_time=#{et.modifiedTime},</if>
  //<if test="et['dr'] != null">dr=#{et.dr},</if>
  //<if test="et['ytenantId'] != null">ytenant_id=#{et.ytenantId},</if>
  //</if>
  //<if test="ew != null and ew.sqlSet != null">${ew.sqlSet}</if>
  //</set>
  //<if test="ew != null">
  //<where>
  //<if test="ew.entity != null">
  //<if test="ew.entity.id != null">id=#{ew.entity.id}</if>
  //<if test="ew.entity['userName'] != null"> AND user_name=#{ew.entity.userName}</if>
  //<if test="ew.entity['age'] != null"> AND age=#{ew.entity.age}</if>
  //<if test="ew.entity['creationTime'] != null"> AND creation_time=#{ew.entity.creationTime}</if>
  //<if test="ew.entity['creator'] != null"> AND creator=#{ew.entity.creator}</if>
  //<if test="ew.entity['pubts'] != null"> AND pubts=#{ew.entity.pubts}</if>
  //<if test="ew.entity['modifier'] != null"> AND modifier=#{ew.entity.modifier}</if>
  //<if test="ew.entity['modifiedTime'] != null"> AND modified_time=#{ew.entity.modifiedTime}</if>
  //<if test="ew.entity['dr'] != null"> AND dr=#{ew.entity.dr}</if>
  //<if test="ew.entity['ytenantId'] != null"> AND ytenant_id=#{ew.entity.ytenantId}</if>
  //</if>
  //<if test="ew.sqlSegment != null and ew.sqlSegment != '' and ew.nonEmptyOfWhere">
  //<if test="ew.nonEmptyOfEntity and ew.nonEmptyOfNormal"> AND</if> ${ew.sqlSegment}
  //</if>
  //</where>
  //<if test="ew.sqlSegment != null and ew.sqlSegment != '' and ew.emptyOfWhere">
  // ${ew.sqlSegment}
  //</if>
  //</if> <if test="ew != null and ew.sqlComment != null">
  //${ew.sqlComment}
  //</if>
  //</script>
  CLICKHOUSE_UPDATE_EXTEND("updateClickhouseExtend", "根据 whereEntity 条件，更新记录",
      "<script>\n ALTER TABLE %s UPDATE %s %s %s\n</script>"),
  CLICKHOUSE_DELETE_EXTEND("deleteClickhouseExtend", "根据 whereEntity 条件，删除记录",
      "<script>\n ALTER TABLE %s DELETE %s %s\n</script>"),
  ;


  private final String method;

  private final String desc;

  private final String sql;

  ExtendMybatisCommand(String method, String desc, String sql) {
    this.method = method;
    this.desc = desc;
    this.sql = sql;
  }

  @Override
  public String getMethod() {
    return method;
  }

//  public String getDesc() {
//    return desc;
//  }

  @Override
  public String getSql() {
    return sql;
  }

//  public static boolean isJoin(String method) {
//    return Stream.of(SELECT_COUNT_EXTEND, SELECT_LIST_EXTEND, SELECT_PAGE_EXTEND)
//        .map(ExtendMybatisCommand::getMethod)
//        .anyMatch(m -> m.equals(method));
//  }

  public static boolean isExtendStatement(String methodName) {
    return Stream.of(values())
        .map(ExtendMybatisCommand::getMethod)
        .anyMatch(statement -> statement.equals(methodName));
  }

  public static boolean isSelectCount(String methodName) {
    return Stream.of(SELECT_COUNT_EXTEND.getMethod(), SqlMethod.SELECT_COUNT.getMethod()).anyMatch(
        methodName::contains);
  }
}
